﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SIS_Wcf_Server.Model;

namespace SIS_Wcf_Server.Search
{
    /// <summary>
    /// Decoractor for a Searcher.
    /// Adds the behaivour to a ISearch implementation that related nets with larger radius will be added to the list of nets used for the search.
    /// </summary>
    public class ExtendedSearchDecorator : ISearch  
    {
        ISearch     _inner;
        IData       _data;
        string      _sourcetype;
        string      _destinationtype;

        public ExtendedSearchDecorator( Model.IData data, ISearch inner, string sourcetype, string destinationtype )
        {
            _data = data;
            _inner = inner;
            _sourcetype = sourcetype;
            _destinationtype = destinationtype;
        }


        #region ISearch Members

        public List<Model.RestInformation> DoSearch(List<Model.Lokation> locations)
        {
            List<Lokation> additionals = new List<Lokation>();

            foreach (var loc in locations)
            {
                // If net type is one that has higher capabilities
                if (loc.Type == _sourcetype)
                {
                    // Find related nets, if this net is 
                    foreach (net n in _data.GetNetForLocationByType(loc, _destinationtype))
                    {
                        // If related net is not in the incomming locations
                        //     convert to Lokation and add to search.
                        if (locations.FindAll( l => l.Value == n.Value ).Count == 0)
                        {
                            Lokation newLocation = new Lokation();
                            newLocation.Value = n.Value;
                            newLocation.Type = n.Type;
                            locations.Add(newLocation);
                        }
                    }
                }
            }
            return _inner.DoSearch(locations);
        }

        public List<Model.RestInformation> DoListAll()
        {
            return _inner.DoListAll();
        }

        #endregion
    }
}